﻿@page
@using SXFramWork.Web
@{
    ViewData["Title"] = "代码生成";
    Layout = AdminLayout.Pjax(HttpContext);
}
<div id="container">


    <div class="list-wall">
        <div class="layui-form list-search">


            <button type="button" class="layui-btn layui-btn-normal" data-type="CreatCode">
                <i class="layui-icon layui-icon-add-circle"></i>代码生成
            </button>

            <button type="button" class="layui-btn layui-btn-info" data-type="reload">
                <i class="layui-icon layui-icon-refresh"></i> 刷新
            </button>
        </div>
        <table class="layui-hide" id="tablelist" lay-filter="tblist"></table>


    </div>

    <script type="text/html" id="tools">
        <a class="layui-btn  layui-btn-normal " lay-event="show" action-code="admin_sysuser_update">查看</a>
    </script>


    <script>
            layui.use(['jquery', 'common','table'],
                function () {
                        $ = layui.jquery,
                        os = layui.common,
                        table = layui.table

                    //加载表格数据
                    os.tableRender({
                        elem: '#tablelist',
                        url: "/api/admin/codegenerator/list",
                        parseData: function (res) { //res 即为原始返回的数据
                            return {
                                "code": res.code == 1 ? 0 : 1,
                                "msg": res.msg,
                                "data": res.data //解析数据列表
                            };
                        },
                        page: false,
                        cols: [
                            [
                                { type: 'checkbox', fixed: 'left' },
                                { field: 'tableName', title: '表名', sort: true, fixed: 'left' },
                                { field: 'tableConmment', title: '表说明' },
                                { title: '操作', width: 160, templet: '#tools' }
                            ]
                        ]
                    })

                    var active = {

                        reload: function () {
                            os.tableReload('tablelist',
                                { where: {}, page: false }
                            )
                        },
                        //生成代码
                        CreatCode: function () {

                            var checkStatus = table.checkStatus('tablelist')
                                , data = checkStatus.data;

                            if (data.length === 0) {
                                os.error("请至少选择一个数据表!");
                                return;
                            }
                            var str = '';
                            $.each(data, function (i, item) {
                                str += item.tableName + ",";
                            });
                            str = str.substring(0, str.length - 1);

                            var url = "/api/admin/codegenerator/createCode/";
                            os.xhrSend(url, 'post', str);
                         
                        }
                       
                    };
                    $('.list-search .layui-btn').on('click', function () {
                        var type = $(this).data('type');
                        active[type] ? active[type].call(this) : '';
                    });

                    //监听工具条
                    table.on('tool(tblist)', function (obj) {
                        var data = obj.data;
                        if (obj.event === 'show') {

                            os.Open('查看' + data.tableName + '数据表', '/admin/codegenerator/tablecolumns/?tableName=' + data.tableName, '900px', '500px', function () {

                            })


                        }
                    });
                  
                });
    </script>

</div>